package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.MemberPackageConfig;
import com.ruoyi.system.domain.MemberPackageItem;
import com.ruoyi.system.domain.vo.MemberPackageItemVo;
import com.ruoyi.system.mapper.MemberPackageItemMapper;
import com.ruoyi.system.service.IMemberItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberItemServiceImpl  extends ServiceImpl<MemberPackageItemMapper, MemberPackageItem>  implements IMemberItemService {
    @Override
    public void addMemberItemList(List<MemberPackageItemVo> itemList, Long id) {
        for (MemberPackageItemVo memberPackageItemVo : itemList) {
            MemberPackageItem memberPackageItem = new MemberPackageItem();
            BeanUtils.copyProperties(memberPackageItemVo, memberPackageItem);
            memberPackageItem.setConfigId(id);
            baseMapper.insert(memberPackageItem);
        }
    }

    @Override
    public void updateByConfig(List<MemberPackageItemVo> itemList, Long id) {
        for (MemberPackageItemVo memberPackageItemVo : itemList) {
            // 修改
            LambdaUpdateWrapper<MemberPackageItem> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(MemberPackageItem::getItemId, memberPackageItemVo.getItemId());
            wrapper.set(MemberPackageItem::getState,0);
            baseMapper.update(null,wrapper);
            MemberPackageItem memberPackageItem1 = new MemberPackageItem();
            BeanUtils.copyProperties(memberPackageItemVo, memberPackageItem1);
            memberPackageItem1.setItemId(null);
            memberPackageItem1.setConfigId(id);
            baseMapper.insert(memberPackageItem1);
        }
    }

    @Override
    public List<MemberPackageItemVo> getListByConfigId(Long id) {
        List<MemberPackageItem> memberPackageItems = baseMapper.selectList(new LambdaQueryWrapper<MemberPackageItem>()
            .eq(MemberPackageItem::getConfigId, id)
            .eq(MemberPackageItem::getState, 1));
        List<MemberPackageItemVo> vos = new ArrayList<>();
        for (MemberPackageItem memberPackageItem : memberPackageItems) {
            MemberPackageItemVo memberPackageItemVo = new MemberPackageItemVo();
            BeanUtils.copyProperties(memberPackageItem, memberPackageItemVo);
            vos.add(memberPackageItemVo);
        }
        return vos;
    }
}
